<?php
/**
 * Created by PhpStorm.
 * User: xiaosq
 * Date: 2018/6/22
 * Time: 11:29
 */

namespace App\Http\Controllers\Api\Meeting;


use App\Http\Controllers\Api\ApiController;
use App\Models\Meeting;
use Illuminate\Http\Request;
use App\Handlers\Oss;
use Validator;
use Lang;

class MeetingsController extends  ApiController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Meeting $meeting)
    {
        $per_page = $request->input('per_page', $this->per_page);

        $data = $meeting->select('id', 'meeting_name', 'meeting_image', 'sign_in_start_at', 'sign_in_end_at', 'sign_up_num', 'sign_in_num', 'status', 'created_at')->orderBy('id', 'DESC')->paginate($per_page);

        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $now = time();

                $sign_in_start_at_str = strtotime($val->sign_in_start_at);
                $sign_in_end_at_str = strtotime($val->sign_in_end_at);
                $status = $val->status;


                if ($now > $sign_in_end_at_str) {
                    $status = 3;
                }

                if ($now >= $sign_in_start_at_str && $now <= $sign_in_end_at_str) {
                    $status = 2;
                }

                $data[$key]->meeting_image_url = Oss::getPublicObjectURL($val->meeting_image);
                $data[$key]->status = $status;
            }
        }

        return $this->getResponseSuccess($data);
    }
}